package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MovieHotSearchAdapter;
import flc.ast.adapter.SearchRecordAdapter;
import flc.ast.databinding.ActivityMessageSearchBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MessageSearchActivity extends BaseAc<ActivityMessageSearchBinding> {
    private MovieHotSearchAdapter hotSearchAdapter;
    private SearchRecordAdapter recordAdapter;
    private String key;

    @Override
    protected int onCreate() {
        return R.layout.activity_message_search;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivSearchBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivSearch.setOnClickListener(this);
        mDataBinding.tvClearRecord.setOnClickListener(this);
        mDataBinding.tvChange.setOnClickListener(this);

        FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(this);
        layoutManager.setFlexDirection(FlexDirection.ROW);
        layoutManager.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvHotSearchList.setLayoutManager(layoutManager);
        hotSearchAdapter = new MovieHotSearchAdapter();
        mDataBinding.rvHotSearchList.setAdapter(hotSearchAdapter);
        hotSearchAdapter.setOnItemClickListener(this);

        // 设置搜索记录列表
        FlexboxLayoutManager layoutManager1 = new FlexboxLayoutManager(this);
        layoutManager1.setFlexDirection(FlexDirection.ROW);
        layoutManager1.setJustifyContent(JustifyContent.FLEX_START);
        mDataBinding.rvRecordList.setLayoutManager(layoutManager1);
        recordAdapter = new SearchRecordAdapter();
        mDataBinding.rvRecordList.setAdapter(recordAdapter);
        recordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivSearch://搜索
                key = mDataBinding.etSearch.getText().toString().trim();
                if (key.isEmpty()) {
                    ToastUtils.showShort(getString(R.string.please_input_movie_name));
                    return;
                }
                saveRecord();
                MessageSearchResultActivity.key = key;
                startActivity(MessageSearchResultActivity.class);
                break;
            case R.id.tvClearRecord://清空
                if (recordAdapter.getItemCount() == 0) return;
                List<String> listSave = new ArrayList<>();
                AppSpUtils.saveMessageSearchRecordList(listSave);
                ToastUtils.showShort(getString(R.string.delete_duc));
                getRecord();
                break;
            case R.id.tvChange://换一换
                getHotSearchData();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == recordAdapter) {
            key = recordAdapter.getItem(position);
        } else {
            StkResBean bean = (StkResBean) adapter.getItem(position);
            BaseWebviewActivity.open(mContext, bean.getUrl(), bean.getName());
        }
        mDataBinding.etSearch.setText(key);
    }

    @Override
    protected void initData() {
        getHotSearchData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        getRecord();
    }

    private void getHotSearchData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_MESSAGE_SEARCH_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotSearchAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void saveRecord() {
        List<String> listSave = new ArrayList<>();
        listSave.add(key);
        List<String> listSp = AppSpUtils.getMessageSearchRecordList();
        if (listSp != null && listSp.size() > 0) {
            listSp.remove(key);
            listSp.add(0, key);
            AppSpUtils.saveMessageSearchRecordList(listSp);
        } else {
            AppSpUtils.saveMessageSearchRecordList(listSave);
        }
    }

    private void getRecord() {
        List<String> listSp = AppSpUtils.getMessageSearchRecordList();
        if (listSp != null && listSp.size() > 0) {
            recordAdapter.setList(listSp);
            mDataBinding.rvRecordList.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.rvRecordList.setVisibility(View.GONE);
        }
    }
}